

/*
load and clean model estimates/experiments
from julia output

output:
- ../data/model/target_moments.dta: model/data target moments
- ../data/model/localization*.dta: model/data localization coeffs
- ../data/model/experiments_*.dta: baseline QE/QT model output
- ../data/model/qe_exp_*.dta: robustness

*/



********************************************************************************
********************************************************************************

* read and merge model moments, across high/low
cap program drop read_model_moments 
program define read_model_moments

* parameter arguments
syntax, fname_low(string) fname_high(string) sheetnames(string) ///
	[dta_fname(string)]

local est_low_fname "../../model_nkph/output/`fname_low'.xlsx"
local est_high_fname "../../model_nkph/output/`fname_high'.xlsx"

if "`dta_fname'" == "" {
	local dta_fname "target_moments"
}

* merge all moments
tempfile model_data
local merge_data 0
foreach sheetname in `sheetnames' {
	import excel `est_low_fname', sheet("`sheetname'") firstrow clear
	keep tau bhat b
	rename bhat bhat_`sheetname'
	rename b b_`sheetname'
	if `merge_data'==1 {
		merge 1:1 tau using `model_data', nogen
	}
	save `model_data', replace
	* merge after first read
	local merge_data 1
}

* save
save ../data/model/`dta_fname'.dta, replace


end


********************************************************************************

* read and merge model coefficients, across short/long and high/low
cap program drop read_model_localization_coeffs 
program define read_model_localization_coeffs

* parameter arguments
syntax, fname_low(string) fname_high(string) ///
	sheetname_short(string) sheetname_long(string) keepvars(string) ///
	[dta_fname(string)]

local est_low_fname "../../model_nkph/output/`fname_low'.xlsx"
local est_high_fname "../../model_nkph/output/`fname_high'.xlsx"

if "`dta_fname'" == "" {
	local dta_fname "localization_coeffs"
}

tempfile model_data
import excel `est_low_fname', sheet("`sheetname_short'") firstrow clear
keep tau `keepvars'
foreach v of varlist `keepvars' {
	rename `v' `v'_short_low
}
save `model_data', replace

import excel `est_low_fname', sheet("`sheetname_long'") firstrow clear
keep tau `keepvars'
foreach v of varlist `keepvars' {
	rename `v' `v'_long_low
}
merge 1:1 tau using `model_data', nogen
save `model_data', replace

import excel `est_high_fname', sheet("`sheetname_short'") firstrow clear
keep tau `keepvars'
foreach v of varlist `keepvars' {
	rename `v' `v'_short_high
}
merge 1:1 tau using `model_data', nogen
save `model_data', replace

import excel `est_high_fname', sheet("`sheetname_long'") firstrow clear
keep tau `keepvars'
foreach v of varlist `keepvars' {
	rename `v' `v'_long_high
}
merge 1:1 tau using `model_data', nogen

* save
save ../data/model/`dta_fname'.dta, replace

end



********************************************************************************
* read model exercises
cap program drop read_qe_model 
program define read_qe_model

* parameter arguments
syntax, qe_fname(string) ///
	[dta_fname_stub(string)]

local xls_fname "../../model_nkph/output/`qe_fname'.xlsx"

if "`dta_fname'" == "" {
	local dta_fname_stub `qe_fname'
}

* yield curve response
tempfile qe_data
* QE
import excel `xls_fname', sheet("qe_yc") firstrow clear
save `qe_data', replace

* MP
import excel `xls_fname', sheet("mp_yc") firstrow clear
ds tau, not
foreach v of varlist `r(varlist)' {
	rename `v' `v'_mp
}
merge 1:1 tau using `qe_data', nogen

* save
save ../data/model/`dta_fname_stub'_yc.dta, replace


* IRFs
tempfile qe_data
* QE
import excel `xls_fname', sheet("qe_yt_irf") firstrow clear
save `qe_data', replace

import excel `xls_fname', sheet("qe_xt_irf") firstrow clear
merge 1:1 t using `qe_data', nogen
save `qe_data', replace

* MP
import excel `xls_fname', sheet("mp_xt_irf") firstrow clear
ds t, not
foreach v of varlist `r(varlist)' {
	rename `v' `v'_mp
}
merge 1:1 t using `qe_data', nogen

* save
save ../data/model/`dta_fname_stub'_irf.dta, replace

end


********************************************************************************
* read and merge model experiments
cap program drop merge_qe_experiments 
program define merge_qe_experiments

* parameter arguments
syntax, experiment_fname_stub(string) ///
	experiment_sheetname(string) experiment_vars(string) ///
	[dta_fname(string)]

local QE_fname "../../model_nkph/output/`experiment_fname_stub'.xlsx"
local QE_safeonly_fname "../../model_nkph/output/`experiment_fname_stub'_safeonly.xlsx"
local QE_riskyonly_fname "../../model_nkph/output/`experiment_fname_stub'_riskyonly.xlsx"

if "`dta_fname'" == "" {
	local dta_fname `experiment_sheetname'
}
tempfile qe_data
* baseline
import excel `QE_fname', sheet(`experiment_sheetname') firstrow clear
* for active/passive experiment: rescale
if "`experiment_sheetname'" == "qe_exp_active_passive" {
	gen `experiment_vars' = (_n-1) / (_N-1)
}
save `qe_data', replace

* safe-only
import excel `QE_safeonly_fname', sheet(`experiment_sheetname') firstrow clear
* for active/passive experiment: rescale
if "`experiment_sheetname'" == "qe_exp_active_passive" {
	gen `experiment_vars' = (_n-1) / (_N-1)
}
ds `experiment_vars', not
foreach v of varlist `r(varlist)' {
	rename `v' `v'_safeonly
}
merge 1:1 `experiment_vars' using `qe_data', nogen
save `qe_data', replace

* risky-only
import excel `QE_riskyonly_fname', sheet(`experiment_sheetname') firstrow clear
* for active/passive experiment: rescale
if "`experiment_sheetname'" == "qe_exp_active_passive" {
	gen `experiment_vars' = (_n-1) / (_N-1)
}
ds `experiment_vars', not
foreach v of varlist `r(varlist)' {
	rename `v' `v'_riskyonly
}
merge 1:1 `experiment_vars' using `qe_data', nogen


* save
save ../data/model/`dta_fname'.dta, replace

end



********************************************************************************
********************************************************************************
n di "reading baseline model output"

n di "model moments and localization coeffs"
* moments
read_model_moments, fname_low("low_crisis_fit") fname_high("high_crisis_fit") ///
	sheetnames("D12_y_D12_ishort_0" "D12_y" "D1_y_D1_ishort_0" "D1_y") ///
	dta_fname("target_moments")

* baseline localization coeffs
read_model_localization_coeffs, fname_low("low_crisis_fit") fname_high("high_crisis_fit") ///
	sheetname_short("y_ilong_5") sheetname_long("y_ilong_6") ///
	keepvars(b) dta_fname("localization_baseline")

* alt localization coeffs
read_model_localization_coeffs, fname_low("low_crisis_fit") fname_high("high_crisis_fit") ///
	sheetname_short("alt_phreg_5") sheetname_long("alt_phreg_6") ///
	keepvars(b) dta_fname("alt_localization")

* risky localization coeffs
read_model_localization_coeffs, fname_low("low_crisis_fit") fname_high("high_crisis_fit") ///
	sheetname_short("risky_phreg_5") sheetname_long("risky_phreg_6") ///
	keepvars(y y_til) dta_fname("risky_localization")


n di "QE/QT models"
read_qe_model, qe_fname("experiments_QE1")
read_qe_model, qe_fname("experiments_QT_passive_med")




********************************************************************************
********************************************************************************
n di "reading QE model experiments"

* model experiments
local exp_names qe_exp_mean_reversion ///
	qe_exp_policy_volatility ///
	qe_exp_maturity_dur ///
	qe_exp_active_passive ///
	qe_exp_risk_aversion ///
	qe_exp_payoff_risk ///
	qe_exp_effrate_shape ///
	qe_exp_effrate_weight ///
	qe_exp_mp_pi ///
	qe_exp_mp_x ///
	qe_exp_mp_reversion ///
	qe_exp_pc_rigidity ///
	qe_exp_dividend_x ///
	qe_exp_dividend_pi ///
	qe_exp_dividend_reversion ///
	
	
local exp_vars kappa_QE ///
	sigma_QE ///
	theta1_QE ///
	active_passive_frac ///
	a ///
	s_d ///
	eta1_til ///
	n0 ///
	p_ipi ///
	p_ix ///
	k_i ///
	delta_x ///
	p_dx ///
	p_dpi ///
	k_d

local N_experiments: word count `exp_names'

forval i=1/`N_experiments' {
	local exp_name: word `i' of `exp_names'
	local exp_var: word `i' of `exp_vars'
	* experiments with multiple params
	if "`exp_name'" == "qe_exp_maturity_dur" {
		local exp_var theta1_QE theta1_til_QE
	}
	* read and save
	merge_qe_experiments, experiment_fname_stub(experiments_QE1) ///
		experiment_sheetname(`exp_name') experiment_vars(`exp_var')
	
}



